package controller.services.impl;

import controller.services.JLayerManagerService;
import controller.services.PlaylistPersistenceService;
import controller.services.PresetPersistenceService;
import controller.services.VTPlayerService;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javazoom.jl.decoder.Equalizer;
import model.PlayModeTypes;
import model.mp3.MP3Info;
import model.mp3.MP3Object;
import model.mp3.impl.MP3ObjectImpl;
import model.preset.Preset;
import model.preset.impl.PresetImpl;

public class VTPlayerServiceImpl implements VTPlayerService {

    private static JLayerManagerService jLayerService = new JLayerManagerServiceImpl();
    private static PlaylistPersistenceService playlistPersistenceService = new PlaylistPersistenceServiceImpl();
    private static PresetPersistenceService presetPersistenceService = new PresetPersistenceServiceImpl();

    private List<MP3Object> playlist = new ArrayList<>();
    private Preset preset = new PresetImpl();
    private MP3Object songToPlay;
    private Equalizer egalizor;

    @Override
    public void initializeSystemData() {

        // load the playlist
        this.playlist = playlistPersistenceService.readData();

        // load the preset data
        this.preset = presetPersistenceService.readData();
        setSongToPlay(preset.getLastSongPlayed());

        //initailize the equalizer
        egalizor = new Equalizer();
    }

    @Override
    public void play(MP3Object mp3Object) {
        System.out.println("Accesing play on VTPlayerService");

        preset.setLastSongPlayed(mp3Object);
        presetPersistenceService.saveData(preset);

        jLayerService.loadFile(mp3Object.getMp3File());
        jLayerService.play();
    }

    @Override
    public void loadFile(File mp3File) {

        // retrieve song info and add to playlist
        MP3Object mp3object = new MP3ObjectImpl();
        jLayerService.loadFile(mp3File);
        MP3Info info = getSongInfo();
        System.out.println("song info " + info);

        mp3object.setMp3Info(info);
        mp3object.setMp3File(mp3File);

        playlist.add(mp3object);

        //persist the playlist 
        playlistPersistenceService.saveData(playlist);

        setSongToPlay(mp3object);

        //TODO update playlist in view
    }

    @Override
    public void stop() {
        jLayerService.stop();
        System.out.println("Accesing stop on VTPlayerService");
    }

    @Override
    public void pause() {
        jLayerService.pause();
        System.out.println("Accesing pause on VTPlayerService");
    }

    @Override
    public void setVolume(Float gain) {
        jLayerService.setVolume(gain);
        System.out.println("Accesing volslider on VTPlayerService");
        preset.setVolume(gain);
        presetPersistenceService.saveData(preset);
    }

    @Override
    public void setEqForBand(int band, Float gain) {
        System.out.println("Previous gain: " + egalizor.getBand(band) + " for band no: " + band);
        egalizor.setBand(band, gain);
        System.out.println("Setting gain: " + gain + " for band no: " + band);
    }

    @Override
    public void mute() {
        jLayerService.mute();
        System.out.println("Accesing mute on VTPlayerService");
    }

    @Override
    public void playNextSong() {
        jLayerService.playNextSong();

        System.out.println("Accesing playnext on VTPlayerService");
    }

    @Override
    public void playPreviousSong() {
        jLayerService.playPreviousSong();
        System.out.println("Accesing playprevious on VTPlayerService");
    }

    @Override
    public void setPlayMode(PlayModeTypes playMode) {
        jLayerService.setPlayMode(playMode);
        System.out.println("Accesing playmode on VTPlayerService");
    }

    @Override
    public List<MP3Object> getPlaylist() {
        System.out.println("Accesing getplaylist on VTPlayerService");
        if (playlist == null) {
            playlist = playlistPersistenceService.readData();
        }
        return playlist;

    }

    @Override
    public Preset getPreset() {
        return preset;
    }

    @Override
    public MP3Info getSongInfo() {
        return jLayerService.getSongInfo();

    }

    @Override
    public MP3Object getSongToPlay() {
        return this.songToPlay;
    }

    private void setSongToPlay(MP3Object songtoplay) {

        this.songToPlay = songtoplay;
        jLayerService.loadFile(songToPlay.getMp3File());
        MP3Info info = getSongInfo();
        this.songToPlay.setMp3Info(info);
        preset.setLastSongPlayed(songToPlay);
        presetPersistenceService.saveData(preset);
    }
}
